package com.tian.manager.controller;


import com.tian.manager.entity.vo.LoginManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;
import com.tian.commonutils.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tian
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/manager")
@CrossOrigin
public class SysManagerController {

    @PostMapping("/login")
    public Result login(@RequestBody LoginManager loginManager){
        try {
            String username = loginManager.getUsername();
            String password =loginManager.getPassword();
            password = new Md5Hash(password, username, 3).toString();
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
            subject.login(usernamePasswordToken);

            String sessionId = (String) subject.getSession().getId();
            return Result.ok().message("登录成功！").data("sessionId",sessionId);

        }catch (Exception e){
            return Result.error().message("用户名或密码错误！");
        }
    }


}

